var http = require("http");
var path = require("path");
var fs = require("fs");

function getFile(filename, res){
	fs.readFile(filename, function(err, contents){
		if(!err){
			res.end(contents);
		}else{
			res.writeHead(500);
			res.end();
		}
	});
}

http.createServer(function(req, res){
	console.log("version 1");
	var filename = path.basename(req.url) || "index.html";
	var ext = path.extname(filename);
	var localpath = __dirname + "/public/";
	
	if(ext == ".html"){
		localpath += filename;
		fs.exists(localpath,function(exists){
			if(exists){
				getFile(localpath, res);			
			}else{
				res.writeHead(404);
				res.end();
			}
		});
	}
	
}).listen(8000);